/**
 * @file       EventHandler_test.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 12, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "EventHandler.h"
#include "gtest/gtest.h"
#include "typedefine.h"
#include "errorcode.h"
#include "KeyboardEvent.h"
#include "KeyboardListener.h"
#include "MockKeyboardEvent.h"
#include "MockEventHandler.h"
namespace NS_LCD_AFW
{
class CEventHandler_test : public ::testing::Test
{
public:
	virtual void SetUp()
	{
	}
	virtual void TearDown()
	{
	}
};
TEST_F(CEventHandler_test,test_defualtConstruction)
{
}
TEST_F(CEventHandler_test,test_addEventListener)
{
	CEventHandler *pHandler = new CEventHandler();
	EXPECT_EQ(ERR_INVALID_LISTENER,pHandler->addEventListener(NULL));
	EXPECT_EQ(ERR_SUCCESS,pHandler->addEventListener(new CKeyboardListener()));
	OBJ_DELETE(pHandler);
}
TEST_F(CEventHandler_test,test_removeEventListener)
{
	CEventHandler *pHandler = new CEventHandler();
	IEventListener *pkbListener =  new CKeyboardListener();
	EXPECT_EQ(ERR_SUCCESS, pHandler->addEventListener(pkbListener));
	EXPECT_EQ(ERR_SUCCESS,pHandler->removeEventListener(pkbListener));
	EXPECT_EQ(NULL,pkbListener);
	EXPECT_EQ(ERR_INVALID_LISTENER,pHandler->removeEventListener(pkbListener));
	OBJ_DELETE(pHandler);
}
TEST_F(CEventHandler_test,test_notify)
{
	CEventHandler *pHandler = new CEventHandler();
	IEvent *pEvent =   new CMockKeyboardEvent();

	EXPECT_EQ(ERR_NO_EVENT_LISTENER,pHandler->notify(pEvent));

	IEventListener *pkbListener = new CKeyboardListener(pHandler);
	EXPECT_EQ(ERR_SUCCESS, pHandler->addEventListener(pkbListener));

	EXPECT_EQ(ERR_EVENT_UN_HANDLED,pHandler->notify(NULL));

	EXPECT_EQ(ERR_EVENT_UN_HANDLED,pHandler->notify(pEvent));

	OBJ_DESTORY(pEvent);
	OBJ_DELETE(pHandler);
	pkbListener = NULL;

}
TEST_F(CEventHandler_test,test_broadcast)
{
	CEventHandler *pHandler = new CEventHandler();
	IEvent *pEvent = new CMockKeyboardEvent();

	EXPECT_EQ(ERR_NO_EVENT_LISTENER, pHandler->broadcast(pEvent));

	IEventListener *pkbListener = new CKeyboardListener();
	EXPECT_EQ(ERR_SUCCESS, pHandler->addEventListener(pkbListener));

	EXPECT_EQ(ERR_SUCCESS, pHandler->broadcast(NULL));

	EXPECT_EQ(ERR_SUCCESS, pHandler->broadcast(pEvent));

	OBJ_DESTORY(pEvent);
	OBJ_DELETE(pHandler);
	pkbListener = NULL;

}
TEST_F(CEventHandler_test,test_handler)
{
	CEventHandler *pHandler = new CEventHandler();
	IEvent *pEvent = new CMockKeyboardEvent();

	EXPECT_EQ(ERR_INVALID_EVENT, pHandler->handler(NULL));

	EXPECT_EQ(ERR_NO_EVENT_LISTENER, pHandler->handler(pEvent));

	IEventListener *pkbListener = new CKeyboardListener(pHandler);
	EXPECT_EQ(ERR_SUCCESS, pHandler->addEventListener(pkbListener));

	EXPECT_EQ(ERR_EVENT_UN_HANDLED, pHandler->handler(pEvent));


	OBJ_DESTORY(pEvent);
	OBJ_DELETE(pHandler);
	pkbListener = NULL;
}
TEST_F(CEventHandler_test,test_handlerEvent)
{
	CEventHandler *pHandler = new CEventHandler();

	EXPECT_EQ(ERR_INVALID_EVENT, pHandler->handlerEvent(NULL));

	IEvent *pEvent = new CMockKeyboardEvent();
	EXPECT_EQ(ERR_EVENT_UN_HANDLED, pHandler->handlerEvent(pEvent));

	pHandler->registerCallBack(pEvent->getEventId(),NULL);
	EXPECT_EQ(ERR_EVENT_UN_HANDLED, pHandler->handlerEvent(pEvent));

	OBJ_DELETE(pHandler);

	pHandler = new CMockEventHandler();
	pHandler->registerCallBack(pEvent->getEventId(),(AFW_EventCallBack)&CMockEventHandler::eventCallBack);
	EXPECT_EQ(ERR_EVENT_UN_HANDLED, pHandler->handlerEvent(pEvent));

	OBJ_DELETE(pHandler);
	OBJ_DESTORY(pEvent);
}
TEST_F(CEventHandler_test,test_getCallBack)
{
	CEventHandler *pHandler = new CMockEventHandler();
	pHandler->registerCallBack(123,(AFW_EventCallBack)&CMockEventHandler::eventCallBack);

	EXPECT_EQ((AFW_EventCallBack)&CMockEventHandler::eventCallBack,pHandler->getCallBack(123));
	OBJ_DELETE(pHandler);
}
TEST_F(CEventHandler_test,test_registerCallBack)
{
	CEventHandler *pHandler = new CMockEventHandler();
	pHandler->registerCallBack(123,(AFW_EventCallBack)&CMockEventHandler::eventCallBack);
	EXPECT_EQ((AFW_EventCallBack)&CMockEventHandler::eventCallBack,pHandler->getCallBack(123));
	OBJ_DELETE(pHandler);
}
TEST_F(CEventHandler_test,test_release)
{
}

} /* namespace NS_LCD_AFW */
